package de.gwtprime.client;

import java.util.Date;

import org.moxieapps.gwt.highcharts.client.Point;
import org.moxieapps.gwt.highcharts.client.Series;
import org.moxieapps.gwt.highcharts.client.Series.Type;
import org.moxieapps.gwt.highcharts.client.StockChart;

import com.google.gwt.i18n.client.DateTimeFormat;

public class ChartTester {


	/**
	 * Generate a simple candlestick charts with some example data
	 * @return
	 */
	public StockChart generateTestChart(){
		int size = 3;

		Number[][] numbers = new Number[size][5];

		numbers[0] = new Number[]{generateDate(2012,12,01).getTime()+43200000,5000,5500,4000,4500};
		numbers[1] = new Number[]{generateDate(2012,12,02).getTime()+43200000,3000,5500,6000,4500};
		numbers[2] = new Number[]{generateDate(2012,12,03).getTime()+43200000,4000,5500,6000,4500};
		final StockChart chart = new StockChart()
		.setChartTitleText("Test Chart")
		.setType(Type.CANDLESTICK);


		Series ohlc = chart.createSeries()
				.setName("OHLC Data Points")
				.setPoints(numbers);
		chart.addSeries(ohlc);

		addFlag(chart, ohlc);
		return chart;
	}
	
	/**
	 * Add a sample flag to the chart
	 * @param chart
	 * @param series
	 */
	public void addFlag(StockChart chart, Series series){
		chart.addSeries(chart.createSeries()
			    .setOption("type", "flags")
			    .setOption("onSeries", series.getOptions().get("id"))
			    .setOption("shape", "flag")
			    .setOption("width", 16)
			    .setPoints(new Point[]{
			        new Point(generateDate(2012,12,01).getTime()+43200000,5000)
			            .setOption("title", "bla")
			            .setOption("text", "test"),
			       
			    })
			);
	}

	static final DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat("yyyy-MM-dd");
	private Date generateDate(int year, int month, int day) {
		return dateTimeFormat.parse(year + "-" + month + "-" + day);
	}
}
